﻿package com.mui.controls
{
    import com.gengine.game.*;
    import com.gengine.global.*;
    import com.gengine.resource.*;
    import com.mui.core.*;
    import com.mui.events.*;
    import com.mui.manager.*;
    import com.mui.utils.*;
    import fl.controls.*;
    import flash.display.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.geom.*;
    import flash.utils.*;

    public class GLoadingButton extends Button implements IToolTipItem, IFrUI, IData
    {
        public var extName:String;
        public var isDisableOverSkin:Boolean = false;
        private var _isMouseOver:Boolean = false;
        private var _my9Gride:Rectangle;
        private var _isDisposed:Boolean;
        protected var _resName:String = "";
        protected var _isHasInitStyle:Boolean = false;
        protected var _toolTipData:Object;
        private var _reactionTime:int = 500;
        private var _Data:Object;
        private static var _dicUrlRefer:Dictionary = new Dictionary();
        private static const colorFilter2:ColorMatrixFilter = new ColorMatrixFilter([0.5086, 0.2094, 0.082, 0, 0, 0.5086, 0.2094, 0.082, 0, 0, 0.5086, 0.2094, 0.082, 0, 0, 0, 0, 0, 1, 0]);

        public function GLoadingButton()
        {
            this.buttonMode = true;
            this.useHandCursor = true;
            this.tabChildren = false;
            var _loc_1:* = new Bitmap();
            setStyle("overSkin", _loc_1);
            setStyle("upSkin", _loc_1);
            setStyle("downSkin", _loc_1);
            setStyle("disabledSkin", _loc_1);
            setStyle("disabledTextFormat", GLoadedButton.diableTextFormat);
            this.addEventListener(Event.ADDED_TO_STAGE, this.judgeToolTip);
            this.addEventListener(Event.REMOVED_FROM_STAGE, this.judgeToolTip);
            return;
        }// end function

        protected function judgeToolTip(event:Event = null) : void
        {
            if (event && event.type == Event.ADDED_TO_STAGE && this.toolTipData || !event && this.toolTipData && Global.stage.contains(this))
            {
                ToolTipsManager.register(this);
            }
            else
            {
                ToolTipsManager.unregister(this);
            }
            if (event && event.type == Event.ADDED_TO_STAGE)
            {
                this.configEventListener(MouseEvent.MOUSE_OVER, this.showMouseHand);
                this.configEventListener(MouseEvent.MOUSE_OUT, this.hideMouseHand);
            }
            if (event && event.type == Event.REMOVED_FROM_STAGE && this._isMouseOver)
            {
                this.hideMouseHand();
            }
            return;
        }// end function

        protected function showMouseHand(event:MouseEvent) : void
        {
            if (this.enabled)
            {
                this._isMouseOver = true;
            }
            return;
        }// end function

        protected function hideMouseHand(event:MouseEvent = null) : void
        {
            this._isMouseOver = false;
            GameGlobal.mouseBtnOutFunction.call();
            return;
        }// end function

        public function get my9Gride() : Rectangle
        {
            return this._my9Gride;
        }// end function

        public function set my9Gride(param1:Rectangle) : void
        {
            this._my9Gride = param1;
            return;
        }// end function

        public function get styleName() : String
        {
            return this._resName;
        }// end function

        public function set styleName(param1:String) : void
        {
            var _loc_2:Bitmap = null;
            if (this._resName)
            {
                LoaderHelp.removeResEvent(this._resName, this.setStyles);
            }
            if (!param1)
            {
                _loc_2 = new Bitmap();
                removeReference(this._resName);
                setStyle("overSkin", _loc_2);
                setStyle("upSkin", _loc_2);
                setStyle("downSkin", _loc_2);
                setStyle("disabledSkin", _loc_2);
                setStyle("selectedDisabledSkin", _loc_2);
                this._resName = "";
                return;
            }
            removeReference(this._resName);
            this._resName = param1.replace("_upSkin", "");
            addReference(this._resName);
            LoaderHelp.addResCallBack(this._resName, this.setStyles);
            return;
        }// end function

        protected function setStyles() : void
        {
            if (!GlobalClass.hasRes(this._resName + "_upSkin"))
            {
                return;
            }
            setStyle("overSkin", this.getBimap("_overSkin"));
            setStyle("upSkin", this.getBimap("_upSkin"));
            setStyle("downSkin", this.getBimap("_downSkin"));
            setStyle("disabledSkin", this.getBimap("_disabledSkin"));
            setStyle("selectedUpSkin", this.getBimap("_selectedUpSkin"));
            this._isHasInitStyle = true;
            this.dispatchEvent(new MuiEvent(MuiEvent.GLOADEDBUTTON_STYLE_COMPLETE));
            return;
        }// end function

        public function get isHasInitStyle() : Boolean
        {
            return this._isHasInitStyle;
        }// end function

        protected function getBimap(param1:String) : Bitmap
        {
            var _loc_3:String = null;
            var _loc_2:* = this._resName + param1;
            if (GlobalClass.hasRes(_loc_2))
            {
                if (this._my9Gride != null)
                {
                    return GlobalClass.getScaleBitmap(_loc_2, this._my9Gride);
                }
                return GlobalClass.getBitmap(_loc_2);
            }
            else
            {
                _loc_3 = "_upSkin";
                if (param1 == "_disabledSkin" && this.isDisableOverSkin)
                {
                    _loc_3 = "_overSkin";
                }
                if (param1 == "_selectedUpSkin" && this.isDisableOverSkin)
                {
                    _loc_3 = "_overSkin";
                }
                if (this._my9Gride != null)
                {
                    return GlobalClass.getScaleBitmap(this._resName + _loc_3, this._my9Gride);
                }
                return GlobalClass.getBitmap(this._resName + _loc_3);
            }
        }// end function

        public function set filterEnabled(param1:Boolean) : void
        {
            this.enabled = param1;
            this.filters = param1 ? (null) : ([colorFilter2]);
            return;
        }// end function

        public function get toolTipData()
        {
            return this._toolTipData;
        }// end function

        public function set toolTipData(param1) : void
        {
            this._toolTipData = param1;
            this.judgeToolTip();
            return;
        }// end function

        public function get reactionTime() : int
        {
            return this._reactionTime;
        }// end function

        public function set reactionTime(param1:int) : void
        {
            this._reactionTime = param1;
            return;
        }// end function

        public function configEventListener(param1:String, param2:Function, param3:Boolean = false, param4:int = 0, param5:Boolean = false) : void
        {
            ObjEventListerTool.addObjEvent(this, param1, param2, param3);
            addEventListener(param1, param2, param3, param4, param5);
            return;
        }// end function

        public function set isDisposed(param1:Boolean) : void
        {
            this._isDisposed = param1;
            return;
        }// end function

        override public function removeEventListener(param1:String, param2:Function, param3:Boolean = false) : void
        {
            ObjEventListerTool.removeObjEvent(this, param1, param2, param3);
            super.removeEventListener(param1, param2, param3);
            return;
        }// end function

        public function dispose(param1:Boolean = true) : void
        {
            if (this._isDisposed && param1)
            {
                if (Global.isDebugModle)
                {
                    Alert.show("存在组件二次释放" + this.toString());
                }
                return;
            }
            if (this._resName)
            {
                LoaderHelp.removeResEvent(this._resName, this.setStyles);
                this._resName = "";
            }
            this._isHasInitStyle = false;
            this.extName = "";
            this.mouseEnabled = true;
            this.filterEnabled = true;
            this.isDisableOverSkin = false;
            toggle = false;
            selected = false;
            this.styleName = "";
            setStyle("disabledTextFormat", GLoadedButton.diableTextFormat);
            ObjEventListerTool.delObjEvent(this);
            this._my9Gride = null;
            if (this.parent)
            {
                this.parent.removeChild(this);
            }
            if (param1)
            {
                UICompomentPool.disposeUICompoment(this);
            }
            this._reactionTime = 500;
            return;
        }// end function

        public function get Data()
        {
            return this._Data;
        }// end function

        public function set Data(param1) : void
        {
            this._Data = param1;
            return;
        }// end function

        private static function removeReference(param1:String) : void
        {
            if (_dicUrlRefer.hasOwnProperty(param1))
            {
                if (_dicUrlRefer[param1] > 0)
                {
                    var _loc_2:* = _dicUrlRefer;
                    var _loc_3:* = param1;
                    var _loc_4:* = _dicUrlRefer[param1] - 1;
                    _loc_2[_loc_3] = _loc_4;
                }
            }
            return;
        }// end function

        private static function addReference(param1:String) : void
        {
            if (_dicUrlRefer.hasOwnProperty(param1))
            {
                var _loc_2:* = _dicUrlRefer;
                var _loc_3:* = param1;
                var _loc_4:* = _dicUrlRefer[param1] + 1;
                _loc_2[_loc_3] = _loc_4;
            }
            else
            {
                _dicUrlRefer[param1] = 1;
            }
            return;
        }// end function

        public static function diposeBitmap() : void
        {
            var _loc_1:String = null;
            for (_loc_1 in _dicUrlRefer)
            {
                
                if (_dicUrlRefer[_loc_1] == 0)
                {
                    disposeBimap(_loc_1, "_overSkin");
                    disposeBimap(_loc_1, "_upSkin");
                    disposeBimap(_loc_1, "_downSkin");
                    disposeBimap(_loc_1, "_disabledSkin");
                    disposeBimap(_loc_1, "_selectedUpSkin");
                    disposeBimap(_loc_1, "_emphasizedSkin");
                    delete _dicUrlRefer[_loc_1];
                }
            }
            return;
        }// end function

        private static function disposeBimap(param1:String, param2:String) : void
        {
            var _loc_3:* = param1 + param2;
            if (GlobalClass.hasRes(_loc_3))
            {
                GlobalClass.disposeBitmapData(_loc_3);
            }
            return;
        }// end function

    }
}
